﻿/*
 * File: TeamLogo.cs
 * Author: Gourav Das
 * Purpose: Model for PhotoFrames
 * Created: May 31 2011
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using _22yards.Core;

namespace _22yards.ThreeD
{
    /// <summary>
    /// This is a game component that implements CustomModel.
    /// </summary>
    public class TeamLogoModel : CustomModel
    {
        /// <summary>
        /// Occurs when Object is Mouse Left clicked Selected
        /// </summary>
        public event EventHandler MouseLeftClick;

        /// <summary>
        /// Occurs when [mouse right key selected].
        /// </summary>
        public event EventHandler MouseRightClick;

        /// <summary>
        /// Occurs when [mouse left double clicks].
        /// </summary>
        public event EventHandler MouseLeftDoubleClick;

        /// <summary>
        /// Gets or sets the player ID of the player for this Photoframe
        /// </summary>
        /// <value>
        /// The player ID.
        /// </value>
        public Team TeamID
        {
            get;
            set;
        }

        private int rotDir; //0 No rotn, 1 right rotn, 2 left rotn
        private float angle = 0;

        private float rotSpeed = 0.01f;

        /// <summary>
        /// Gets or sets the rotation speed.
        /// </summary>
        /// <value>
        /// The oscillation speed.
        /// </value>
        public float RotationSpeed
        {
            get
            {
                return rotSpeed;
            }
            set
            {
                rotSpeed = value;
            }
        }

        /// <summary>
        /// Resets the oscillation speed.
        /// </summary>
        public void ResetRotationSpeed()
        {
            rotSpeed = 0.4f;
        }


        /// <summary>
        /// Gets the rot dir. 0 - No Rotation, 1 - left, 2 - right
        /// </summary>
        public int RotDir
        {
            get
            {
                return rotDir;
            }
        }

        /// <summary>
        /// Gets the current angle in rotation
        /// </summary>
        public float Angle
        {
            get
            {
                return angle;
            }
        }

        private bool isMouseLeftClickSelected = false;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is selected.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is selected; otherwise, <c>false</c>.
        /// </value>
        public bool IsMouseLeftClickSelected
        {
            get
            {
                return isMouseLeftClickSelected;
            }
            set
            {
                isMouseLeftClickSelected = value;
                if (this.MouseLeftClick != null && isSelectable && isMouseLeftClickSelected == true)
                    this.MouseLeftClick(this, new EventArgs());
            }
        }

        private bool isMouseRightClickSelected = false;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is selected.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is selected; otherwise, <c>false</c>.
        /// </value>
        public bool IsMouseRightClickSelected
        {
            get
            {
                return isMouseRightClickSelected;
            }
            set
            {
                isMouseRightClickSelected = value;
                if (this.MouseRightClick != null && isSelectable && isMouseRightClickSelected == true)
                    this.MouseRightClick(this, new EventArgs());
            }
        }

        private bool isMouseLeftDoubleClickSelected;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is mouse left double click selected.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is mouse left double click selected; otherwise, <c>false</c>.
        /// </value>
        public bool IsMouseLeftDoubleClickSelected
        {
            get
            {
                return isMouseLeftDoubleClickSelected;
            }
            set
            {
                isMouseLeftDoubleClickSelected = value;
                if (this.MouseLeftDoubleClick != null && isSelectable && isMouseLeftDoubleClickSelected == true)
                    this.MouseLeftDoubleClick(this, new EventArgs());
            }
        }

        private bool isSelectable = false;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is selectable.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is selectable; otherwise, <c>false</c>.
        /// </value>
        public bool IsSelectable
        {
            get
            {
                return isSelectable;
            }
            set
            {
                isSelectable = value;
            }
        }

        private bool rotate = true;
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="PhotoFrameModel"/> will oscillate.
        /// </summary>
        /// <value>
        ///   <c>true</c> if oscillate; otherwise, <c>false</c>.
        /// </value>
        public bool Rotate
        {
            get
            {
                return rotate;
            }
            set
            {
                rotate = value;
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PhotoFrameModel"/> class.
        /// </summary>
        /// <param name="_Model">The _ model.</param>
        /// <param name="_Position">The _ position.</param>
        /// <param name="_Rotation">The _ rotation.</param>
        /// <param name="_Scale">The _ scale.</param>
        /// <param name="_graphicsDevice">The _graphics device.</param>
        /// <param name="_Name">Name of the _.</param>
        /// <param name="_ID">The _ ID.</param>
        /// <param name="_RotateDir">The oscillate.</param>
        public TeamLogoModel(Model _Model, Vector3 _Position, Vector3 _Rotation,
            Vector3 _Scale, GraphicsDevice _graphicsDevice, string _Name, short _ID, int _RotateDir)
            : base(_Model,
                _Position, _Rotation, _Scale, ref _graphicsDevice, _Name, _ID)
        {
            rotDir = _RotateDir;
            angle = (rotDir == 1) ? 0 : 360;
        }

        private void RotatePhotoFrame()
        {
            Rotation = new Vector3(0, (float)MathHelper.ToRadians(angle), 0);
            if (rotDir == 1)
            {
                if (angle > 360f)
                {
                    angle = 0;
                }
                else
                {
                    angle += 1;
                }
            }
            else if(rotDir ==2)
            {
                if (angle < 0f)
                    angle = 360f;
                else
                    angle -= 1;
            }
        }

        /// <summary>
        /// Syncs the oscillations with another model.
        /// </summary>
        /// <param name="_model">The _model.</param>
        public void SyncRotationWithModel(TeamLogoModel _model)
        {
            rotDir = _model.RotDir;
            angle = _model.Angle;
        }

        /// <summary>
        /// Updates this instance.
        /// </summary>
        public override void Update()
        {
            if (rotDir > 0 && rotate)
                RotatePhotoFrame();

            base.Update();
        }
    }

}
